package com.cqjtu.store.product.service.impl;

import com.cqjtu.store.common.PageVO;
import com.cqjtu.store.common.ServiceCode;
import com.cqjtu.store.common.ServiceException;
import com.cqjtu.store.favorite.mapper.FavoriteMapper;
import com.cqjtu.store.favorite.pojo.entity.Favorite;
import com.cqjtu.store.product.mapper.ProductCategoryMapper;
import com.cqjtu.store.product.mapper.ProductMapper;
import com.cqjtu.store.product.pojo.dto.ProductCategoryPageDTO;
import com.cqjtu.store.product.pojo.entity.Product;
import com.cqjtu.store.product.pojo.entity.ProductCategory;
import com.cqjtu.store.product.pojo.vo.ProductDetailVO;
import com.cqjtu.store.product.pojo.vo.ProductListVO;
import com.cqjtu.store.product.pojo.vo.ProductVO;
import com.cqjtu.store.product.service.ProductService;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: cqjtu-store
 * @Titile: ProductServiceImpl
 * @Author: Lucky
 * @Description: 商品业务层
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private FavoriteMapper favoriteMapper;

    @Override
    public List<ProductVO> getNewList() {
        List<Product> newList = productMapper.getNewList();
        List<ProductVO> productVOList = toConvert(newList);
        return productVOList;
    }
    @Override
    public List<ProductVO> getHotList() {
        List<Product> hotList = productMapper.getHotList();
        List<ProductVO> productVOList = toConvert(hotList);
        return productVOList;
    }

    @Override
    public PageVO<List<ProductListVO>> getListByCategoryId(ProductCategoryPageDTO categoryPageDTO,UserLoginVO userLoginVO) {
        //判断该商品分类id是否存在
        ProductCategory productCategory = productCategoryMapper.getById(categoryPageDTO.getCategoryId());
        if(productCategory == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该商品分类不存在");
        }
        //没有分页的
        //List<Product> productList = productMapper.getListByCategoryId(categoryPageDTO.getCategoryId());
        //SQL语句进行分页的
        List<Product> productList = productMapper.getProductByPage(categoryPageDTO);
        if(productList == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该商品分类没有商品");
        }

        //封装成列表vo（分页中的data）
        List<ProductListVO> productListVOList = new ArrayList<>();

        productList.forEach(product -> {
            ProductListVO productListVO = new ProductListVO();
            productListVO.setId(product.getId());
            productListVO.setTitle(product.getTitle());
            productListVO.setImage(product.getImage());
            productListVO.setPrice(product.getPrice());
            //判断这系列的商品是否被收藏
            if(userLoginVO != null){
                //登录了
                Favorite favorite = favoriteMapper.getFavorite(product.getId(), userLoginVO.getId());
                if(favorite!=null){
                    //设置收藏
                    productListVO.setIsFavorite(1);
                }
            }
            productListVOList.add(productListVO);
        });
        //商品数量
        int count = productMapper.count(productCategory.getId());
        PageVO<List<ProductListVO>> pageVO = new PageVO<>(categoryPageDTO.getPageIndex(),
                categoryPageDTO.getPageSize(), count,productListVOList);
        return pageVO;
    }

    @Override
    public PageInfo<Product> getListByCategoryId(ProductCategoryPageDTO categoryPageDTO) {
        //判断该商品分类id是否存在
        ProductCategory productCategory = productCategoryMapper.getById(categoryPageDTO.getCategoryId());
        if(productCategory == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该商品分类不存在");
        }
        //开启分页：设置分页参数:页码值；每页显示的数量
        PageHelper.startPage(categoryPageDTO.getPageIndex(),categoryPageDTO.getPageSize());
        //调用持久层方法
        List<Product> productList = productMapper.getListByCategoryId(categoryPageDTO.getCategoryId());
        if(productList == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该商品分类没有商品");
        }
        //封装成vo ：分页会失效，分页插件不会对sql语句进行拦截处理的
        // List<ProductVO> productVOList = toConvert(productList);
        //分页封装
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        return pageInfo;

    }

    @Override
    public PageVO<List<ProductVO>> getListByCategoryId3(ProductCategoryPageDTO categoryPageDTO) {
        //判断该商品分类id是否存在
        ProductCategory productCategory = productCategoryMapper.getById(categoryPageDTO.getCategoryId());
        if(productCategory == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该商品分类不存在");
        }
        //调用持久层方法
        List<Product> productList = productMapper.getProductByPage(categoryPageDTO);
        if(productList == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该商品分类没有商品");
        }
        List<ProductVO> productVOList = toConvert(productList);
        int count = productMapper.count(categoryPageDTO.getCategoryId());
        PageVO<List<ProductVO>> pageVO = new PageVO<>(categoryPageDTO.getPageIndex(), categoryPageDTO.getPageSize(),count,productVOList);
        return pageVO;
    }

    /**
     *  根据id查询商品详情
     * @param id
     * @param userLoginVO
     * @return
     */
    @Override
    public ProductDetailVO getById(Integer id, UserLoginVO userLoginVO) {
        //判断这个id是否合法
        Product product =  productMapper.getById(id);
        if(product==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"没有此商品");
        }
        //有商品
        Favorite favorite = null;
        if(userLoginVO != null){
            //登录过，是否被当前用户收藏了
            Integer userId = userLoginVO.getId();
            //根据商品编号以及用户编号去查询收藏表，检查该商品是否被收藏
            favorite = favoriteMapper.getFavorite(id, userId);
        }
        //封装vo
        ProductDetailVO detailVO = new ProductDetailVO();
        detailVO.setId(product.getId());
        detailVO.setTitle(product.getTitle());
        detailVO.setImage(product.getImage());
        detailVO.setPrice(product.getPrice());
        detailVO.setSellPoint(product.getSellPoint());
        if(favorite != null){
            //被收藏了
            detailVO.setIsFavorite(1);
        }
        return detailVO;
    }

    //封装的转换方法
    private List<ProductVO> toConvert(List<Product> productList){
        List<ProductVO> productVOList = new ArrayList<>();
        productList.forEach(product -> {
            ProductVO productVO = new ProductVO();
            productVO.setId(product.getId());
            productVO.setTitle(product.getTitle());
            productVO.setPrice(product.getPrice());
            productVO.setImage(product.getImage());
            productVOList.add(productVO);
        });
        return productVOList;
    }

}
